﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;

namespace LCWMS
{
    public partial class w_inv050 : DockContent
    {
        public w_inv050()
        {
            InitializeComponent();
        }
        private DataSet dscustomer;
        private BarcodeLib.Barcode b_bar = new BarcodeLib.Barcode();
        private ReportDocument rd_report = new ReportDocument();
        private string g_str_label = "";
        private string g_str_type = "";

        private void w_inv050_Load(object sender, EventArgs e)
        {
            dscustomer = new DataSet();
            //初始化货主编码 
            try
            {
                dscustomer = main.g_class.comm_get_allconsignor();
              
                cbx_customer.ValueMember = "consignor";
                cbx_customer.DisplayMember = "consignor";
                DataRow drnew;
                drnew = dscustomer.Tables[0].NewRow();
                drnew["consignor"] = "";
                drnew["consignor_name"] = "";
                dscustomer.Tables[0].Rows.Add(drnew);
                cbx_customer.DataSource = dscustomer.Tables[0];
                if (dscustomer.Tables[0].Rows.Count > 0) tb_custname.Text = dscustomer.Tables[0].Rows[0][1].ToString().Trim();

            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Consignor's Information", main.g_info_dataset) + "!" + ee.ToString());
                return;
            }
            //标签打印是否显示条码信息'  g_str_label
            try
            {
                DataSet ds_id;
                string str_sql = "";

                if (main.dbtype == "SQL")
                {
                    str_sql = "select isnull(id_data,'') id_data from " + main.g_space + ".sys_parameter where inv_org='" + main.g_org + "'and id in(8104,8113) order by id";
                    ds_id = main.dbservice.Sql_exec_selectsql(str_sql);
                }
                else
                {
                    str_sql = "select nvl(id_data,'') id_data from " + main.g_space + ".sys_parameter where inv_org='" + main.g_org + "'and id in(8104,8113) order by id";
                    ds_id = main.dbservice.oracle_exec_selectsql(str_sql);
                }
                if (ds_id.Tables[0].Rows.Count > 0)
                {
                    g_str_label = ds_id.Tables[0].Rows[0]["id_data"].ToString().Trim();
                    g_str_type = ds_id.Tables[0].Rows[1]["id_data"].ToString().Trim();
                }
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(ee.ToString());
            }
            //语言转换
            main.g_class.comm_changeControl(this, main.g_win_dataset);

            if (main.g_report.Trim() == "")
            {
                rd_report.Load(main.g_path + "\\REPORT\\inv050_rpt.rpt");
                //this.asn012_rept1.Load("..//..//asn012_rept.rpt");              
            }
            else
            {
                rd_report.Load(main.g_path + "\\" + main.g_report.Trim().ToUpper() + "_REPORT\\inv050_rpt_" + main.g_report.Trim() + ".rpt");
                //this.asn012_rept1.Load("..//..//asn012_rept_" + main.g_report.Trim() + ".rpt");
            }
            crystalReportViewer1.ReportSource = rd_report;           
        }

        private void btn_query_Click(object sender, EventArgs e)
        {
            DataSet ds_tmp = new DataSet();
            string str_temp="";
            // crv_asn.Refresh();
            // ds_tmp.Reset();
            //得到订单的明细信息
            //if (main.g_report.Trim() == "")
            //{
            if (main.dbtype == "SQL")
            {
                str_temp = " select distinct b.lot_attribute4 asn_no,c.sku,c.sku_name,c.spec,c.type,c.warehouseman,a.location_code,f.class_description sku_class," +
                                " case c.bom_flag when '0' then '否' when '1' then '是' else '' end bom_flag," +
                                " case c.pallet_flag when '1' then 'T'+ rtrim(ltrim(a.pallet_no)) + rtrim(ltrim (c.barcode)) + '' else 'L' + rtrim(ltrim(a.location_code)) + rtrim(ltrim (c.barcode)) + '' end as plu_code, " +
                                "  case d.location_flag when '0' then '正常' when '1' then '退货' when '2' then '坏品' when '3'then '寄售库位' else '' end asn_status," +
                                  "  c.main_unit,c.sub_unit,b.lot_attribute3 as tax_dist,(a.stock_qty_m-a.freeze_qty_m) stock_qty_m,(a.stock_qty_s-a.freeze_qty_s) stock_qty_s,a.consignor,b.lot_attribute5 Batch,'*'+ltrim(rtrim(b.lot_attribute5))+'*' Batch2 " +
                                 " from " + main.g_space + ".inv_sku_pallet a," + main.g_space + ".inv_sku_batchno b," + main.g_space + ".mst_sku c," + main.g_space + ".mst_location d," + main.g_space + ".mst_sku_class f where a.inv_org=b.inv_org and a.inv_org=c.inv_org and a.inv_org=b.inv_org " +
                                 "  and a.batchno=b.batchno  and a.consignor=c.consignor and a.sku=c.sku and a.location_code=d.location_code and a.inv_org=d.inv_org and c.inv_org=f.inv_org  and c.consignor=f.consignor and c.sku_class=f.sku_class and a.inv_org='" + main.g_org + "'  ";
            }
            else
            {
                str_temp = " select distinct b.lot_attribute4 asn_no,c.sku,c.sku_name,c.spec,c.type,c.warehouseman,a.location_code,f.class_description sku_class," +
                                " case c.bom_flag when '0' then '否' when '1' then '是' else '' end bom_flag," +
                                " case c.pallet_flag when '1' then 'T'|| trim(a.pallet_no)||trim (c.barcode)||'' else 'L'|| trim(a.location_code)||trim (c.barcode)||'' end as plu_code, " +
                                "  case d.location_flag when '0' then '正常' when '1' then '退货' when '2' then '坏品' when '3'then '寄售库位' else '' end asn_status," +
                                  "  c.main_unit,c.sub_unit,b.lot_attribute3 as tax_dist,(a.stock_qty_m-a.freeze_qty_m) stock_qty_m,(a.stock_qty_s-a.freeze_qty_s) stock_qty_s,a.consignor,b.lot_attribute5 Batch,'*'||trim(b.lot_attribute5)||'*' Batch2  " +
                                 " from " + main.g_space + ".inv_sku_pallet a," + main.g_space + ".inv_sku_batchno b," + main.g_space + ".mst_sku c," + main.g_space + ".mst_location d," + main.g_space + ".mst_sku_class f where a.inv_org=b.inv_org and a.inv_org=c.inv_org and a.inv_org=b.inv_org " +
                                 "  and a.batchno=b.batchno  and a.consignor=c.consignor and a.sku=c.sku and a.location_code=d.location_code and a.inv_org=d.inv_org and c.inv_org=f.inv_org  and c.consignor=f.consignor and c.sku_class=f.sku_class and a.inv_org='" + main.g_org + "'  ";
            }
            //}
            //else
            //{
            //    str_temp = " select distinct c.sku,c.sku_name,c.spec,a.location_code,c.sku_class," +                            
            //                  " case c.pallet_flag when '1' then 'T'|| trim(a.pallet_no)||trim (c.barcode)||'' else 'L'|| trim(a.location_code)||trim (c.barcode)||'' end as plu_code " +                              
            //                   " from " + main.g_space + ".inv_sku_pallet a," + main.g_space + ".inv_sku_batchno b," + main.g_space + ".mst_sku c," + main.g_space + ".mst_location d where a.inv_org=b.inv_org and a.inv_org=c.inv_org and a.inv_org=b.inv_org " +
            //                   "  and a.batchno=b.batchno  and a.consignor=c.consignor and a.sku=c.sku and a.location_code=d.location_code and a.inv_org=d.inv_org and a.inv_org='" + main.g_org + "'  ";
            //}
                //判断是否有货主
            if (cbx_customer.Text.Trim() != "")
            {
                str_temp = str_temp + " and a.consignor = '" + cbx_customer.Text.Trim() + "'";
            }
            else //没有货主提取有权限的所有货主
            {
                str_temp = str_temp + " and a.consignor in (select e.consignor from " + main.g_space + ".mst_customer e," + main.g_space + ".sys_user f," + main.g_space + ".sys_user_customer t" +
                           " where f.inv_org = t.inv_org and f.user_id = t.user_id and t.inv_org = e.inv_org and t.consignor = e.consignor and f.inv_org = '" + main.g_org + "' and f.user_id = '" + main.g_user + "' and e.consignor_flag='1') ";
            }
            //判断是否根据托盘查找
            if (tb_pallet_no.Text.Trim() != "")
            {
                str_temp += " and a.pallet_no like '%"+tb_pallet_no.Text.Trim()+"%'";
            }
            //判断是否根据库位查找
            if (tb_location_code.Text.Trim() != "")
            {
                str_temp += " and a.location_code like '%" + tb_location_code.Text.Trim() + "%'";
            }
            //判断是否根据商品查找
            if (tb_sku.Text.Trim() != "")
            {
                str_temp += " and a.sku like '%" + tb_sku.Text.Trim() + "%'";
            }
            //判断是否根据商品查找
            if (tb_sku_name.Text.Trim() != "")
            {
                str_temp += " and c.sku_name like '%" + tb_sku_name.Text.Trim() + "%'";
            }
            //判断是否根据商品查找
            if (tb_warehouseman.Text.Trim() != "")
            {    
                if (tb_warehouseman.Text.Trim().ToUpper() == "NULL")
                {
                    str_temp += " and (c.warehouseman is null or c.warehouseman='' ) ";   
                }
                else
                {
                    str_temp += " and c.warehouseman = '" + tb_warehouseman.Text.Trim() + "'";
                }
            }
            if (textBox1.Text.Trim() != "")  
            {
                str_temp += " and b.lot_attribute5='"+textBox1.Text.Trim()+"'";
            }
            if (checkBox1.Checked == true)
            {
                str_temp += " and a.stock_qty_m >0 ";
            }
            str_temp += " order by a.location_code,c.sku ";
            try
            {
                if (main.dbtype == "SQL")
                    ds_tmp = main.dbservice.Sql_exec_selectsql(str_temp);
                else
                    ds_tmp = main.dbservice.oracle_exec_selectsql(str_temp);
                //给数据集加一列存放条码图片
                ds_tmp.Tables[0].Columns.Add("image", System.Type.GetType("System.Byte[]"));
                //ds_tmp.Tables[0].Columns.Add("Batchcode", System.Type.GetType("System.Byte[]"));
                
                if (ds_tmp.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds_tmp.Tables[0].Rows.Count; i++)
                    {
                        if (g_str_label.Trim() == "1")
                        { 
                            b_bar.IncludeLabel = true;
                        }
                        else  
                        {
                            b_bar.IncludeLabel = false;
                        }
                        //===== Encoding performed here =====
                        if (g_str_type.Trim() == "1")
                        {                           
                            b_bar.Encode(BarcodeLib.TYPE.CODE128, ds_tmp.Tables[0].Rows[i]["sku"].ToString().Trim(), System.Drawing.Color.Black, System.Drawing.Color.White, 480, 60);                          
                        }
                        else
                        {
                            if (ds_tmp.Tables[0].Rows[i]["plu_code"].ToString().Trim().Substring(0, 1).ToUpper() == "L")
                            {
                                b_bar.Encode(BarcodeLib.TYPE.CODE128, ds_tmp.Tables[0].Rows[i]["plu_code"].ToString().Trim(), System.Drawing.Color.Black, System.Drawing.Color.White, 480, 60);
                            }
                            else
                            {
                                b_bar.Encode(BarcodeLib.TYPE.CODE128, ds_tmp.Tables[0].Rows[i]["plu_code"].ToString().Trim(), System.Drawing.Color.Black, System.Drawing.Color.White, 420, 60);
                            }
                        }
                        byte[] byteArray = b_bar.GetImageData(BarcodeLib.SaveTypes.PNG); 
     //                   byte[] byteArray = b_bar.GetImageData(BarcodeLib.SaveTypes.BMP);
                            //ms.ToArray();
        //                byteArray = ms.ToArray();
                        ds_tmp.Tables[0].Rows[i]["image"] = byteArray;//将图片存储到dataSet中 
                        //===== Encoding performed here =====
                        //if (ds_tmp.Tables[0].Rows[i]["Batch"].ToString().Trim() != "")
                        //{
                        //    b_bar.IncludeLabel = false;
                        //    ms = new MemoryStream();
                        //    b_bar.Encode(BarcodeLib.TYPE.CODE128, ds_tmp.Tables[0].Rows[i]["Batch"].ToString().Trim(), System.Drawing.Color.Black, System.Drawing.Color.White, 420, 60).Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                        //    byte[] byteArray2 = ms.ToArray();
                        //    byteArray2 = ms.ToArray();
                        //    ds_tmp.Tables[0].Rows[i]["Batchcode"] = byteArray2;//将图片存储到dataSet中 
                        //}
                  
                    }
                }  
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(main.g_class.comm_getchangestring("Fail to get Information of bill's detail", main.g_info_dataset) + "!" + ee.ToString());

                return;
            }
            //this.inv050_rpt1.SetDataSource(ds_tmp.Tables[0]);
            //crystalReportViewer1.ReportSource = inv050_rpt1;

            rd_report.SetDataSource(ds_tmp.Tables[0]);
            crystalReportViewer1.ReportSource = rd_report;
          

        }

        private void cbx_customer_TextChanged(object sender, EventArgs e)
        {
            string str_cust;
            str_cust = cbx_customer.Text.Trim();
            tb_custname.Text = main.g_class.comm_findname(str_cust, dscustomer);
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }
    }
}